CREATE OR REPLACE PACKAGE BODY ut_pck_authorisation
IS
   PROCEDURE ut_setup IS
   BEGIN
     null;
   END;
 
   PROCEDURE ut_teardown
   IS
   BEGIN
     null;
   END;
  
   
   PROCEDURE validateMoreThanAutIndex(
   			 p_aut_root_id number,
			 p_expected_count number
   )
   
   IS
    v_result_count number;
   BEGIN
   select count(*) into v_result_count from autindex 
	where aux_parent_elm_id =  p_aut_root_id ;
	if v_result_count > p_expected_count then 
		utp.utAssert.eq('testing more than '||p_expected_count, v_result_count,v_result_count);
	ELSE 
		 utp.utAssert.eq('testing more than '||p_expected_count, 0,v_result_count);
	end if;
   END;
   
   PROCEDURE validateAutIndex(
   			 p_aut_root_id number,
			 p_expected_count number
   )
   
   IS
    v_result_count number;
   BEGIN
   select count(*) into v_result_count from autindex 
	where aux_parent_elm_id =  p_aut_root_id ;
		utp.utAssert.eq(' i found '||v_result_count||' elements for node: '||p_aut_root_id ||'  in the index table', v_result_count,p_expected_count);
   END;
   
    PROCEDURE ut_moveTest
   IS 
   v_child_node number := pck_element.getElm_IDByBookmark('childnode');
   v_aut_root_node number:=pck_element.getElm_IDByBookmark('aut_root');
   v_containers_cat_id number :=pck_meta.getCat_IdByName('containers','Container','/omscube');
   v_cmp_child_node_id number;
    
   BEGIN
   		savepoint A;
   		select cmp_id into v_cmp_child_node_id from composition where
		cmp_target_elm_id =v_child_node;  		
   		pck_relation.moveCompositionAppend(v_cmp_child_node_id,v_aut_root_node,v_containers_cat_id,1);
		validateAutIndex(v_aut_root_node,6);
		rollback to A;
   END;
   
    PROCEDURE ut_createAuthorisationForRoot
	IS 
	v_test_root_id number;
	v_id number;
	v_child_elm_id number;
	v_parent_aur_id number;
	BEGIN
	v_test_root_id := pck_element.createElement( 'test_root','test_root', pck_meta.getScm_IdByName( 'Container', pck_meta.getNsp_IdByPath( '/omscube' ) ), 0 );		
		v_id := pck_authorisation.createAuthorisation(v_test_root_id, v_test_root_id, '1', '1', '1', '1', '1' , 0 );
		v_id := pck_relation.appendComposition( null, null, v_test_root_id, null, 0 );
		v_parent_aur_id := pck_authorisation.getParentAuxElm_Id(v_test_root_id);
		utp.utAssert.eq('geting himself',v_parent_aur_id,v_test_root_id);
		validateAutIndex(v_test_root_id,1);
	END;
	
	
   PROCEDURE ut_createAuthorisationForChild
   IS
   v_aut_id number;
   v_aut_elm_id number;
  v_test_root_id number;
  v_id number;
  v_parent_aur_id number;
  v_child_elm_id number;
   BEGIN
		 
		
		
		-- ### createing an existing authorisation
   		v_aut_elm_id := pck_element.getElm_IdByBookmark('aut_root');
   		v_aut_id := pck_authorisation.createAuthorisation(v_aut_elm_id,v_aut_elm_id,1,1,1,1,1,1);
		validateAutIndex(v_aut_elm_id,3);
		
		
   END;
   PROCEDURE ut_rebuildIndexForChild
   IS
   v_aut_elm_id number;
   BEGIN
   delete from autindex;
    v_aut_elm_id := pck_element.getElm_IdByBookmark('aut_root');
      pck_authorisation.reBuildIndex();
	  validateAutIndex(v_aut_elm_id,3);
   END;
   PROCEDURE ut_rebuildIndexFull
   IS 
   v_aut_elm_id number;
   BEGIN 
   	  
	  v_aut_elm_id := pck_element.getElm_IdByBookmark('root');
      pck_authorisation.reBuildIndex(v_aut_elm_id);
   	  validateMoreThanAutIndex(v_aut_elm_id,0);
   
   
     
   END;

   PROCEDURE ut_getAncestorAutElm_Id
   IS
   v_root_elm_id number;
   v_child_elm_id number;
   v_ancestor_elm_id number;
	  BEGIN
	  --- #### testing by root
	  
	  v_root_elm_id := pck_element.getElm_IdByBookmark('root');
	  v_ancestor_elm_id := pck_authorisation.getAncestorAutElm_Id(v_root_elm_id);
	  utp.utAssert.eq('testing if root got himself as ancestor',v_root_elm_id,v_ancestor_elm_id);

	  -- #### testing by child #####
	  v_root_elm_id := pck_element.getElm_IdByBookmark('root');
	  v_child_elm_id := pck_element.getElm_IdByBookmark('childnode'); 
	  v_ancestor_elm_id := pck_authorisation.getAncestorAutElm_Id(v_child_elm_id);
	  utp.utAssert.eq('testing if his ancestor is the root',v_ancestor_elm_id, v_root_elm_id);
	  
   END;
END;
/
